package com.heiye.blog.ai.helper;

import com.google.common.collect.Lists;
import com.heiye.blog.ai.advisor.*;
import com.heiye.blog.ai.advisor.impl.CustomDashScopeChatMemoryAdvisor;
import com.heiye.blog.ai.advisor.impl.CustomDashScopeStreamLoggerAndMessage2DBAdvisor;
import com.heiye.blog.ai.domain.mapper.ChatMessageMapper;
import com.heiye.blog.ai.model.dto.AIChatRequest;
import com.heiye.blog.ai.model.vo.AiChatReqVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
 * @author: heiye
 * @date: 2025/11/09 下午10:08
 * @version: v1.0.0
 * @description: 百炼模型 Advisor 帮助类
 */
@Slf4j
@Component
public class DashScopeAdvisorHelper {

    @Resource
    private ChatMessageMapper chatMessageMapper;

    @Resource
    private TransactionTemplate transactionTemplate;

    /**
     * 构建 Advisor 集合
     *
     * @param aiChatRequest
     * @return
     */
    public List<DashScopeAdvisor> buildDashScopeAdvisors(AIChatRequest aiChatRequest) {
        // Advisor 集合
        List<DashScopeAdvisor> advisors = Lists.newArrayList();

        // 添加自定义对话记忆 Advisor（以最新的 50 条消息作为记忆）
        advisors.add(new CustomDashScopeChatMemoryAdvisor(chatMessageMapper, aiChatRequest, 50));

        // 添加自定义打印流式对话日志 Advisor
        advisors.add(new CustomDashScopeStreamLoggerAndMessage2DBAdvisor(chatMessageMapper, aiChatRequest, transactionTemplate));

        return advisors;
    }
}
